package com.cqrt.service.impl;

import com.cqrt.entity.RtTemporaryTask;
import com.cqrt.mapper.RtTemporaryTaskMapper;
import com.cqrt.service.RtTemporaryTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RtTemporaryTaskServiceImpl implements RtTemporaryTaskService {

    @Autowired
    private RtTemporaryTaskMapper rtTemporaryTaskMapper;

    @Override
    @Transactional
    public RtTemporaryTask insert(RtTemporaryTask task) {
        // 检查JOBID是否已存在
        if (existsByJobId(task.getJobid())) {
            throw new RuntimeException("JOBID已存在，不允许新增");
        }
        
        // 插入记录
        int result = rtTemporaryTaskMapper.insert(task);
        if (result > 0) {
            // 返回插入后的记录（这里简单返回传入的对象，实际可以根据需要查询数据库返回完整记录）
            return task;
        } else {
            throw new RuntimeException("插入记录失败");
        }
    }

    @Override
    @Transactional
    public int deleteByJobId(String jobid) {
        return rtTemporaryTaskMapper.deleteByJobId(jobid);
    }

    @Override
    public boolean existsByJobId(String jobid) {
        int count = rtTemporaryTaskMapper.countByJobId(jobid);
        return count > 1;
    }

    @Override
    public List<RtTemporaryTask> selectByPrjId(String prjid) {
        return rtTemporaryTaskMapper.selectByPrjId(prjid);
    }
}